<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParam.majorName"
        placeholder="搜索专业名"
        prefix-icon="el-icon-search"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <CollegeSelect v-model="queryParam.collegeId" class="filter-item" />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        class="filter-item"
        icon="el-icon-refresh-left"
        @click="handleResetFilter"
      >重置</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="majorList"
      border
      highlight-current-row
      @sort-change="sortChangeMajorList"
    >
      <el-table-column label="专业号" prop="id" sortable="custom" />
      <el-table-column label="专业名" prop="majorName" />
      <el-table-column label="所属学院" prop="collegeName" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            plain
            type="primary"
            icon="el-icon-edit"
            @click.native="handleShowUpdateMajorDialog(row)"
          />
          <el-button
            plain
            type="danger"
            icon="el-icon-delete"
            @click="handleDeleteMajor(row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-num.sync="queryParam.pageNum"
      :page-size.sync="queryParam.pageSize"
      @pagination="pageMajorList"
    />

    <el-dialog
      title="修改专业"
      :visible.sync="showDialog"
      :append-to-body="true"
      @close="showDialog = false"
    >
      <el-form ref="majorFrom" label-width="80px" :model="majorFrom" :rules="rules">
        <el-form-item label="所属学院" prop="collegeId">
          <CollegeSelect v-model="majorFrom.collegeId" />
        </el-form-item>
        <el-form-item label="专业号" prop="id">
          <el-input
            ref="id"
            v-model="majorFrom.id"
            :disabled="true"
            type="text"
          />
        </el-form-item>
        <el-form-item label="专业名" prop="majorName">
          <el-input
            ref="majorName"
            v-model="majorFrom.majorName"
            placeholder="请输入专业名"
            type="text"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleUpdateMajor()">确认修改</el-button>
          <el-button @click="showDialog = false">取消修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { CollegeSelect, Pagination } from '@/views/admin/import'
import { majorNamePattern } from '@/utils/validate'
import { pageMajorList, getMajor, updateMajor, deleteMajor } from '@/api/admin/major'

export default {
  name: 'ViewMajor',
  components: { Pagination, CollegeSelect },
  data() {
    return {
      queryParam: {
        pageNum: 1,
        pageSize: 5,
        orderField: undefined,
        orderRule: 'desc',
        collegeId: undefined,
        majorName: ''
      },
      majorFrom: {
        id: '',
        collegeId: undefined,
        majorName: ''
      },
      rules: {
        majorName: [
          { required: true, message: '请输入专业名', trigger: 'blur' },
          { pattern: majorNamePattern, message: '专业名必须为中文不超过30个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      showDialog: false,
      majorList: [],
      total: 0
    }
  },
  created() {
    this.pageMajorList()
  },
  methods: {
    /**
     * 获取专业信息
     */
    async pageMajorList() {
      this.loading = true
      const data = await pageMajorList(this.queryParam)
      this.majorList = data.records
      this.total = data.total
      this.loading = false
    },
    /**
     * 搜索专业信息
     */
    handleFilter() {
      this.queryParam.pageNum = 1
      this.pageMajorList()
    },
    /**
     * 重置搜索条件
     */
    handleResetFilter() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 5,
        orderField: undefined,
        orderRule: 'desc',
        collegeId: undefined,
        majorName: ''
      }
      this.pageMajorList()
    },
    /**
     * 专业信息排序
     * @param data 排序信息
     */
    async sortChangeMajorList(data) {
      const { prop, order } = data
      this.queryParam.orderField = prop
      this.queryParam.orderRule = order === 'ascending' ? 'asc' : 'desc'
      await this.pageMajorList()
    },
    /**
     * 显示修改专业信息对话框
     * @param row 点击行对应的专业信息
     */
    async handleShowUpdateMajorDialog(row) {
      this.majorFrom = await getMajor(row.id)
      this.$nextTick(() => {
        this.showDialog = true
      })
    },
    /**
     * 修改专业信息
     * @param row 点击行对应的专业信息
     */
    handleUpdateMajor(row) {
      this.$refs.majorFrom.validate(async valid => {
        if (valid) {
          await updateMajor(this.majorFrom)
          this.showDialog = false
          this.$message.success('修改专业成功')
          await this.pageMajorList()
        }
      })
    },
    /**
     * 删除专业信息
     * @param row 点击行对应的专业信息
     */
    handleDeleteMajor(row) {
      this.$confirm('此操作将永久删除专业，是否删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteMajor(row.id)
        this.$message.success('删除专业成功')
        await this.pageMajorList()
      }).catch()
    }
  }
}
</script>
